package edu.fzu.fundmgr.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.fzu.fundmgr.domain.Client;
import edu.fzu.fundmgr.service.ClientService;
import edu.fzu.fundmgr.service.FundService;
import edu.fzu.fundmgr.service.impl.ClientServiceImpl;
import edu.fzu.fundmgr.service.impl.FundServiceImpl;

public class ClientMgrServlet extends HttpServlet {

	public void loadAll(HttpServletRequest request, HttpServletResponse response) {
		ClientService clientService = new ClientServiceImpl();
		List<Client> clientList = clientService.loadAll();

		request.setAttribute("clients", clientList);

		try {
			request.getRequestDispatcher("show_clients.jsp").forward(request,
					response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String act = request.getParameter("act");

		if (act.equals("add")) {

			Client client = new Client();

			client.setIdCardNO(request.getParameter("idcard_no"));
			client.setClientName(request.getParameter("client_name"));
			client.setSex(request.getParameter("sex"));
			client.setPhone(request.getParameter("phone"));
			client.setAddress(request.getParameter("address"));
			client.setEMail(request.getParameter("email"));
			client.setHobby(request.getParameter("hobby"));
			client.setCreatedDate(new Date());

			ClientService clientService = new ClientServiceImpl();
			clientService.addClient(client);

			// response.sendRedirect("fundMgr?act=load_all");
			loadAll(request, response);
		} else if (act.equals("load_all")) {
			loadAll(request, response);
		} else if (act.equals("del")) {

			String idCardNO = request.getParameter("idcard_no");

			ClientService clientService = new ClientServiceImpl();
			clientService.removeClient(idCardNO);

			loadAll(request, response);
		} else if (act.equals("get_client")) {

			String idCardNO = request.getParameter("idcard_no");

			ClientService clientService = new ClientServiceImpl();
			request.setAttribute("client", clientService.getClient(idCardNO));

			request.getRequestDispatcher("update_costlog.jsp").forward(request,
					response);
		} else if (act.equals("update")) {

			Client client = new Client();

			client.setIdCardNO(request.getParameter("idcard_no"));
			client.setClientName(request.getParameter("client_name"));
			client.setSex(request.getParameter("sex"));
			client.setPhone(request.getParameter("phone"));
			client.setAddress(request.getParameter("address"));
			client.setEMail(request.getParameter("email"));
			client.setHobby(request.getParameter("hobby"));
			client.setCreatedDate(new Date());

			ClientService clientService = new ClientServiceImpl();
			clientService.updateClient(client);

			loadAll(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
